package com.taxigang.client.uibinder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.shared.TaxiGangInfo;

public class Suggestion extends TaxiGangPanel {

	private static SuggestionUiBinder uiBinder = GWT
	.create(SuggestionUiBinder.class);

	interface SuggestionUiBinder extends UiBinder<Widget, Suggestion> {
	}

	@UiField Button nextButton;
	@UiField Button previousButton;
	@UiField Button joinButton;
	@UiField Button closeButton;
	@UiField Label startAddressLabel;
	@UiField Label destinationAddressLabel;
	@UiField Label journeyBeginsLabel;

	@UiField Label startAddressContentLabel;
	@UiField Label destinationAddressContentLabel;
	@UiField Label journeyBeginsContentLabel;
	@UiField Label headerLabel;
	@UiField Label statusContentLabel;


	public Suggestion() {
		initWidget(uiBinder.createAndBindUi(this));
		//myTaxiGangsText.setText(TaxiGangCreator.taxiGangConstants.sorry());
		startAddressLabel.setText(TaxiGangCreator.taxiGangConstants.startAddress());
		destinationAddressLabel.setText(TaxiGangCreator.taxiGangConstants.enterDestinationAddress());
		journeyBeginsLabel.setText(TaxiGangCreator.taxiGangConstants.departureTime());
		closeButton.setText(TaxiGangCreator.taxiGangConstants.close());

		joinButton.setText(TaxiGangCreator.taxiGangConstants.join());

		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				taxiGangRoute.resetSuggestion();
				taxiGangRoute.show(PanelType.SEARCH);
			}
		});

		nextButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				Session.getInstance().incCurrentSuggestionIndex();
				show();
			}
		});
		
		previousButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				Session.getInstance().decCurrentSuggestionIndex();
				show();
			}
		});

	}

	public void show(){
		super.show();

		int currentSuggestionIndex = Session.getInstance().getCurrentSuggestionIndex();
		TaxiGangInfo[] infoList = Session.getInstance().getTaxiGangSearchResultList();
		if (infoList.length > 0){
			
			if (infoList.length==1){
				headerLabel.setText(TaxiGangCreator.taxiGangConstants.suggestion());
			} else {
				int displayIndex = Session.getInstance().getCurrentSuggestionIndex()+1;
				headerLabel.setText(TaxiGangCreator.taxiGangConstants.suggestions()+" "+displayIndex+" of "+infoList.length);
			}

			TaxiGangInfo taxiganginfo = infoList[currentSuggestionIndex];
			startAddressContentLabel.setText(taxiganginfo.getStart());
			destinationAddressContentLabel.setText(taxiganginfo.getDestination());
			journeyBeginsContentLabel.setText(taxiganginfo.getDepartureTime());

			taxiGangRoute.showSuggestionInMap(taxiganginfo);
			
			boolean closed = "CLOSED".equals(taxiganginfo.getStatus());
			if (closed){
				statusContentLabel.setText(TaxiGangCreator.taxiGangConstants.taxigangHasExpired());
			} else {
				statusContentLabel.setText("");
			}
			joinButton.setEnabled(!closed);
		}

		nextButton.setVisible( infoList.length - currentSuggestionIndex > 1);
		previousButton.setVisible(currentSuggestionIndex > 0);

	}
	public HasClickHandlers getJoinButton() {
		// TODO Auto-generated method stub
		return joinButton;
	}
}
